/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */

package cgl.narada.transport.proxy;

import java.util.Properties;

import cgl.narada.util.logging.Logger;
import cgl.narada.util.logging.LoggerFactory;
import cgl.narada.util.proxy.WinProxyDetection;

public class ProxyInfo {
    final static Logger log = 
	LoggerFactory.getLogger(ProxyInfo.class.getName());

    private String proxyHost;
    private int proxyPort = 80;
    //temp. proxy on Windows doesn't have its own user/password registry
    private String user = System.getProperty("user.name");
    private String domain;
    private String password;

    private void fromProperties(Properties prop) {
	String val;
        val = prop.getProperty("https.proxyHost");
        if (val != null && val.length() > 0) {
	    setProxyHost(val);
	    log.debug("proxy host: "+ val);
        } 

        val = prop.getProperty("https.proxyPort");
        if (val != null && val.length() > 0) {
            try {
                setProxyPort(Integer.parseInt(val));
		log.debug("proxy port: " + val);
            } catch (NumberFormatException e) {
                log.warn("Illegal proxy port number "+val+" will use default "+proxyPort);
            }
        }

        val = prop.getProperty("domain");
        if (val != null) {
            domain = val;
	    log.debug("Domain set to: "+domain);
        }

        val = prop.getProperty("username");
        if (val != null && val.length() > 0) {
	    setUser(val);
            log.debug("username name: "+ val);
        }

        val = prop.getProperty("password");
        if (val != null && val.length() > 0) {
	    setPassword(val);
	    log.debug("[for debug purpose] password: " + val);
        }
    }
    private void autoDetect() {
        WinProxyDetection wrapper = new WinProxyDetection();
        int type = wrapper.DetectProxySettings();
        log.info("Proxy setting type: "+type);

        log.info("Manual Proxy Enabled: "+wrapper.isEnabled(wrapper.PROXY_TYPE_MANUAL));
        log.info("Auto URL enabled: "+wrapper.isEnabled(wrapper.PROXY_TYPE_AUTO_PROXY_URL));

        String manualProxy = wrapper.GetProxyServer();
        log.info("Manual proxy: "+manualProxy);

        String autoURL = wrapper.GetProxyURL();
        log.info("Auto proxy URL: "+autoURL);

        String bypassURL = wrapper.GetProxyBypass();
        log.info("Bypass URL: "+bypassURL);

        String domainName = wrapper.GetDomainWorkgroupName();
        log.info("Domain/Workgroup name: "+domainName);

	if(manualProxy != null && manualProxy.length() > 0) {
	    int colon = manualProxy.indexOf(":");
	    String h = manualProxy.substring(0, colon);
	    String p = manualProxy.substring(colon+1);
	    if (bypassURL==null || bypassURL.length()==0 || 
		bypassURL.indexOf("local") < 0) {
		setProxyHost(h);	    
		setProxyPort(Integer.parseInt(p));
	    }
	}
	if (domainName != null && domainName.length() > 0) {
	    setDomain(domainName);
	}
    }

    /**
     * NB transport gets proxy settings info from either
     * user specified properties or by proxy auto detection methods.
     * Proxy detection relies on browser's proxy settings. Properties
     * precedes detection.  
     * @param prop user specified transport properties
     * 
     */
    public void getProxyInfo(Properties prop) {
	if (prop != null) fromProperties(prop);
        String osname = System.getProperty("os.name");
        log.info("Operating System: "+osname);

        if (osname.toLowerCase().indexOf("windows") < 0) {
            log.info("System is not Windows.  Not doing proxy detection.");
	    return;
        }
	if (getProxyHost() == null) {
	    log.info("begin proxy auto detecting");
	    autoDetect();
	}
	log.warn("test logging");
    }

    public String getProxyHost() {
	return proxyHost;
    }
    
    public int getProxyPort() {
	return proxyPort;
    }

    public String getUser() {
	return user;
    }

    public String getPassword() {
	return password;
    }
    
    public String getDomain() {
	return domain;
    }

    public void setProxyHost(String h) {
	this.proxyHost = h;
    }

    public void setProxyPort(int p) {
	this.proxyPort = p;
    }

    public void setUser(String u) {
	this.user = u;
    }

    public void setPassword(String p) {
	this.password = p;
    }

    public void setDomain(String d) {
	this.domain = d;
    }
}
